package com.hzyb.oldpack.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.hzyb.oldpack.dao.JdbcDao;


@Repository
@Transactional
public class JdbcDaoImpl implements JdbcDao{

	@Resource
	private JdbcTemplate jdbcTemplate;

	private static Logger logger=LoggerFactory.getLogger(JdbcDaoImpl.class);

	//命名适应前端

	@Override
	public List<Map<String, Object>> getShopCategories() {
		return	jdbcTemplate.queryForList("select a.categories_sid,a.categories_name as text,concat(b.file_path,b.file_name,b.file_suffix) as url from "
				+ "shop_categories a inner join shop_file b on a.file_id=b.file_id where a.is_activate='1' and a.main_category='1'");
	}

	public List<Map<String, Object>> getNewProduct(String is_mianNewProduct){
		if(is_mianNewProduct.equals("-1")) {
			return jdbcTemplate.queryForList("SELECT b.commodity_sid,b.merchant_sid,CONCAT(c.file_path,c.file_name,c.file_suffix) AS img,b.name,b.description AS title,"
					+ "b.unit_price AS price FROM shop_new_products a INNER JOIN shop_commodity b ON a.commodity_sid=b.commodity_sid INNER "
					+ "JOIN shop_file c ON a.file_id=c.file_id where a.is_activate='1'");
		}
		return jdbcTemplate.queryForList("SELECT b.commodity_sid,b.merchant_sid,CONCAT(c.file_path,c.file_name,c.file_suffix) AS img,b.name,b.description AS title,"
				+ "b.unit_price AS price FROM shop_new_products a INNER JOIN shop_commodity b ON a.commodity_sid=b.commodity_sid INNER "
				+ "JOIN shop_file c ON a.file_id=c.file_id where a.is_activate='1' and ifnull(a.is_mianNewProduct,'0')=?",new Object[]{is_mianNewProduct});
	}

	@Override
	public List<Map<String, Object>> getitemInformation(String categories_sid) {
		return jdbcTemplate.queryForList("select a.commodity_sid,name,unit_price,concat(b.file_path,b.file_name,b.file_suffix) as url from shop_commodity a inner join "
				+ "shop_file b on a.file_id=b.file_id where a.categories_sid=?",new Object[] {categories_sid});
	}

	public List<Map<String, Object>> getAllCategories() {
		return jdbcTemplate.queryForList("select distinct a.categories_sid,a.categories_name from shop_categories a inner join shop_commodity b on a.categories_sid"
				+ "=b.categories_sid where a.is_activate='1'");
	}

	@Override
	public List<Map<String, Object>> getShopCart(String userId) {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select a.cart_sid,b.commodity_sid,concat(c.file_path,c.file_name,c.file_suffix) as img,a.num,b.description as title,b.unit_price as price from shop_cart a inner join shop_commodity b on a.commodity_sid=b.commodity_sid left join shop_file c on b.file_id=c.file_id where a.userid=?",new Object[] {userId});
	}

	@Override
	public List<Map<String, Object>> getShopRecommendList() {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select b.unit_price as price,b.name as title,b.description as name,concat(c.file_path,c.file_name,c.file_suffix) as img from shop_recommend_list a inner join shop_commodity b on a.commodity_sid"
				+ "=b.commodity_sid inner join shop_file c on b.file_id=c.file_id");
	}

	@Override
	public List<Map<String, Object>> getAllOrderList(String openId) {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select a.*,concat(c.file_path,c.file_name,c.file_suffix) as img from shop_order a left join shop_file c on a.file_id=c.file_id where a.userid=? and a.state<>'-1'",new Object[] {openId});
	}

	@Override
	public List<Map<String, Object>> getShopPictureTableByBusinessSid(String businessSid) {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select concat(b.file_path,b.file_name,b.file_suffix) as url from shop_picture_table a inner join shop_file b on a.file_id=b.file_id where a.business_sid=?",new Object[] {businessSid});
	}

	@Override
	public List<Map<String, Object>> getShopPopAds(String adsType) {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select a.ads_sid,a.title,a.description,concat(b.file_path,b.file_name,b.file_suffix) as picUrl,a.commodity_sid from shop_pop_ads a left join shop_file b on a.file_id=b.file_id where a.type=?",new Object[] {adsType});
	}

	@Override
	public List<Map<String, Object>> queryShop(String keyWord) {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select a.commodity_sid,a.name as title,concat(b.file_path,b.file_name,b.file_suffix) as image,a.unit_price as price from shop_commodity a left join shop_file b on a.file_id=b.file_id where a.name like concat('%',?,'%')",new Object[] {keyWord});
	}

	@Override
	public List<Map<String, Object>> getShopType(String id) {
		// TODO Auto-generated method stub
		return jdbcTemplate.queryForList("select a.name,b.specification_value from shop_product_specifications a inner join shop_order_detail b on a.specification_sid=b.specification_sid where b.order_id=?",new Object[] {id});
	}
	
	public List<Map<String, Object>> getImgBycommoditySid(String commoditySid){
		return jdbcTemplate.queryForList("select concat(b.file_path,b.file_name,b.file_suffix) as img from shop_commodity a inner join shop_file b on a.file_id=b.file_id where a.commodity_sid=?",new Object[] {commoditySid});
	}

}
